package com.cloud.sys.controller;

import com.cloud.sys.common.ResponseWrapper;
import com.cloud.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.codec.digest.DigestUtils;

import java.rmi.MarshalledObject;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;

/**
 * @author zxh
 * @Title: RestTestController
 * @ProjectName sys
 * @Description: TODO
 * @date 2018/10/2810:12
 */
@RestController()
@RequestMapping("/test")
public class RestTestController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @RequestMapping("/world")
    public  String  test(){

        return "hellow world";
    }
    @PostMapping("/addUser")
    public ResponseWrapper addUser(SysUser user){
        ResponseWrapper responseWrapper = jdbcTemplate.execute("insert  into sys_user (id,create_time,user_name,password,real_name) value (?,?,?,?,?)", new PreparedStatementCallback<ResponseWrapper>() {
            @Override
            public ResponseWrapper doInPreparedStatement(PreparedStatement preparedStatement) throws SQLException, DataAccessException {
                preparedStatement.setString(1, UUID.randomUUID().toString().replace("-",""));
                preparedStatement.setDate(2, new java.sql.Date(new Date().getTime()));
                preparedStatement.setString(3, user.getUserName());
                preparedStatement.setString(4, DigestUtils.md5Hex(user.getPassword()));
                preparedStatement.setString(5, user.getRealName());
                return preparedStatement.executeUpdate() > 0 ? ResponseWrapper.success("添加成功") : ResponseWrapper.error("添加失败");
            }
        });
        return responseWrapper;

    }
}
